package com.cat.cc.tag;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioGroup;

import com.cat.cc.taglibrary.view.ImageDotLayout;
import com.cysion.wedialog.WeDialog;

import java.util.ArrayList;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class ImageDotActivity extends AppCompatActivity {

    private ImageDotLayout imageDotLayout;
    private RadioGroup cbMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_dot);
        imageDotLayout = (ImageDotLayout) findViewById(R.id.idl_idl_photo);
        imageDotLayout.setIconDrawable(getResources().getDrawable(R.drawable.icon_location));
        cbMode = (RadioGroup) findViewById(R.id.rgroup);
        cbMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.rb_image){
                    imageDotLayout.setEditMode(false);
                    imageDotLayout.setOnlyShowImage(true);
                }
                if(checkedId==R.id.rb_show_mark){
                    imageDotLayout.setEditMode(false);
                    imageDotLayout.setOnlyShowImage(false);
                }
                if(checkedId==R.id.rb_edit_mark){
                    imageDotLayout.setEditMode(true);
                    imageDotLayout.setOnlyShowImage(false);
                }
            }
        });
        imageDotLayout.setOnImageClickListener(new ImageDotLayout.OnImageClickListener() {
            @Override
            public void onImageClick(final ImageDotLayout.IconBean bean) {
                WeDialog.INSTANCE.normal(ImageDotActivity.this)
                        .setTitle("提示")
                        .setMsg("是否添加标签?")
                        .show(new Function1<DialogFragment, Unit>() {
                            @Override
                            public Unit invoke(DialogFragment aDialogFragment) {
                                //可以一系列处理后再添加标签
                                imageDotLayout.addIcon(bean);
                                aDialogFragment.dismiss();
                                return null;
                            }
                        });
            }
        });
        //设置背景图片
//        imageDotLayout.setImage("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.daimg.com%2Fuploads%2Fallimg%2F140505%2F1-140505004P3.jpg&refer=http%3A%2F%2Fimg.daimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1647740810&t=61973d8f34040a56da5ef3668ff9f71e");
        imageDotLayout.setImage(R.mipmap.test_bg);
        initIcon();
        imageDotLayout.setOnIconClickListener(new ImageDotLayout.OnIconClickListener() {
            @Override
            public void onIconClick(final View v) {
                ImageDotLayout.IconBean bean= (ImageDotLayout.IconBean) v.getTag();
                WeDialog.INSTANCE.normal(ImageDotActivity.this)
                        .setTitle("提示")
                        .setNoText("删除")
                        .setMsg("您点击了标签，标签id是"+bean.id)
                        .clickCancel(new Function1<DialogFragment, Unit>() {
                            @Override
                            public Unit invoke(DialogFragment aDialogFragment) {
                                imageDotLayout.removeIcon(v);
                                return null;
                            }
                        })
                        .show(new Function1<DialogFragment, Unit>() {
                            @Override
                            public Unit invoke(DialogFragment aDialogFragment) {
                                aDialogFragment.dismiss();
                                //可以一系列处理后再添加标签
                                return null;
                            }
                        });
            }
        });
    }

    private void initIcon() {
        final List<ImageDotLayout.IconBean> iconBeanList = new ArrayList<>();
        ImageDotLayout.IconBean bean = new ImageDotLayout.IconBean(0, 0.3f, 0.4f, null);
        iconBeanList.add(bean);
        bean.resId = R.layout.label_icon;
        bean = new ImageDotLayout.IconBean(1, 0.5f, 0.4f, null);
        iconBeanList.add(bean);
        //监听图片是否加载完成
        imageDotLayout.setOnLayoutReadyListener(new ImageDotLayout.OnLayoutReadyListener() {
            @Override
            public void onLayoutReady() {
                imageDotLayout.addIcons(iconBeanList);
                cbMode.check(R.id.rb_edit_mark);
            }
        });
    }
}
